#include "../include/SynchronousFinder.h"

#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

void SynchronousFinder::setFiles(std::vector<const char*> _files) {
    m_files = _files;
};

void SynchronousFinder::setParameters(std::map<const char*, bool> _params) {
    m_parameters = _params;
};

void SynchronousFinder::setPattern(std::string _pattern) {
    m_pattern = _pattern;
}

void SynchronousFinder::find() {
    unsigned        filesCounter = 0;
    std::ifstream   iFileHandler;
    std::string     line;
    char            tmpFilename[100];
    size_t          found;
    size_t          linesCounter = 0;

 /*   std::cout << "\nZe niby szukam: \t" << _pattern << "\r\n";
    for (unsigned i=0; i<m_files.size(); ++i) {
        std::cout << "w pliku \t\t" << m_files[i] << "\r\n";
    }
    std::cout << std::endl;
*/
    while (filesCounter < m_files.size()) {
        strcpy(tmpFilename, "./");
        strcpy(tmpFilename, m_files[filesCounter]);
        iFileHandler.open(tmpFilename, std::ifstream::in);
        if (!iFileHandler.is_open()) {
            std::cout << "Unable to open file " << tmpFilename << ". Skipping. \n ----" << std::endl;
            ++filesCounter;
            continue;
        }
        std::cout << "\r\n" << tmpFilename << "\r\n" << std::endl;

        while (iFileHandler.good()) {
            ++linesCounter;
            getline (iFileHandler,line);
            found = line.find(this->m_pattern);
            if (found != std::string::npos)
                std::cout << linesCounter << ". " << line << std::endl;
        }
        iFileHandler.close();
        memset(&tmpFilename[0], 0, sizeof(tmpFilename));
        ++filesCounter;
    }
};
